import paramiko
import time

def ssh_command(host, port, username, password, commands):
    client = paramiko.SSHClient()
    # 添加已知的主机密钥
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(host, port, username, password)

    # 创建一个持续的 shell 会话
    shell = client.invoke_shell()

    def execute(command):
        shell.send(command + "\n")
        time.sleep(2)  # 等待命令执行
        output = shell.recv(10000).decode()  # 调整缓冲区大小根据需要
        print(output)

    for command in commands:
        execute(command)

    client.close()

if __name__ == "__main__":
    # 示例参数，你应该用实际的值替换它们
    host = '172.16.10.254'
    port = 22
    username = 'admin'
    password = 'gsta@passw0rd'
    commands = ['system-view', 'security-zone name gsta']  # 命令链

    ssh_command(host, port, username, password, commands)
